package com.yld.car.common;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.yld.car.domain.Car;
import com.yld.car.market.R;

public class PortTask extends AsyncTask<String, String, ArrayList<Car>> {

	public PortTask(String methodId, String requestURL, ListView list,
			Context c, ProgressBar pb) {
		this.methodId = methodId;
		this.requestURL = requestURL;
		this.list = list;
		utils = NetworkProgressUtils.getInstance();
		this.mContext = c;
		this.pb = pb;
	}

	private String methodId;
	private String requestURL;
	private ListView list;
	private NetworkProgressUtils utils;
	private Context mContext;
	private ProgressBar pb;

	@Override
	protected ArrayList<Car> doInBackground(String... params) {
		//
		// HashMap<String, String> allParams = new HashMap<String,
		// String>();
		// allParams.put("seriesId", params[0]);
		// 20/16
		Object o = utils.getWebservicesByData(
				ConstantUtils.NAME_SPACE,
				ConstantUtils.ALL_METHROD_NAME.get(Integer.parseInt(methodId)),
				ConstantUtils.NAME_SPACE
						+ ConstantUtils.ALL_METHROD_NAME.get(Integer
								.parseInt(methodId)), requestURL, null);

		// System.out.println("type====" + o);
		ArrayList<Car> all = utils.parseJson(o.toString(), "table",
				NetworkProgressUtils.BASE_CAR_TYPE,false);

		return all;
	}

	@Override
	protected void onPostExecute(ArrayList<Car> result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);

		if (pb != null) {
			pb.setVisibility(View.GONE);
		}
		list.setVisibility(View.VISIBLE);
		ArrayList<String> all = null;
		if (result != null) {

			all = new ArrayList<String>();
			for (Car b : result) {

				all.add(b.getName());
			}
			// mApp.getAllConfigurations().put("" + methodId, result);
			// MyFilterAdapter adapter = new MyFilterAdapter(all, methodId);

			// ArrayAdapter<String> adapter = new ArrayAdapter<String>(
			// getApplicationContext(),
			// // android.R.layout.simple_list_item_multiple_choice,
			// android.R.id.text1, all);

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
					R.layout.base_list_items_simple, R.id.itemContent, all) {

				@Override
				public View getView(int position, View convertView,
						ViewGroup parent) {
					convertView = super.getView(position, convertView, parent);
					SharedPreferences shared = mContext.getSharedPreferences(
							"port_state", Context.MODE_PRIVATE);

					int p = shared.getInt("port", -1);
					if (p == position && pb != null) {
						convertView.setBackgroundColor(Color.RED);
					} else {
						convertView
								.setBackgroundResource(android.R.drawable.list_selector_background);
					}

					return convertView;
				}
			};
			list.setAdapter(adapter);
			// list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		}

	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		if (pb != null) {
			pb.setVisibility(View.VISIBLE);
			list.setVisibility(View.GONE);
		}
	}
}